package com.house365.core.view.pulltorefresh;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ListView;

import com.house365.core.view.pulltorefresh.internal.EmptyViewMethodAccessor;

/**
 * 
 * @modifier Rick Ping
 * 
 */
public class PullToRefreshExpandableListView extends
		PullToRefreshAdapterViewBase<ExpandableListView> {

	class InternalExpandableListView extends ExpandableListView implements
			EmptyViewMethodAccessor {

		public InternalExpandableListView(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		@Override
		public void setEmptyView(View emptyView) {
			PullToRefreshExpandableListView.this.setEmptyView(emptyView);
		}

		@Override
		public void setEmptyViewInternal(View emptyView) {
			super.setEmptyView(emptyView);
		}

		public ContextMenuInfo getContextMenuInfo() {
			return super.getContextMenuInfo();
		}
	}

	public PullToRefreshExpandableListView(Context context) {
		super(context);
	}

	public PullToRefreshExpandableListView(Context context, int mode) {
		super(context, mode);
	}

	public PullToRefreshExpandableListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected final ExpandableListView createRefreshableView(Context context,
			AttributeSet attrs) {
		ExpandableListView lv = new InternalExpandableListView(context, attrs);

		// Set it to this so it can be used in ListActivity/ListFragment
		lv.setId(android.R.id.list);
		return lv;
	}

	@Override
	public ContextMenuInfo getContextMenuInfo() {
		return ((InternalExpandableListView) getRefreshableView())
				.getContextMenuInfo();
	}

	public void onRefreshComplete(CharSequence lastUpdated) {
		mHeaderLayout.setLastUpdate(lastUpdated);
		super.onRefreshComplete();

	}

	public void setFooterViewVisible(int visibility) {
		if (visibility == View.GONE) {
			this.removeView(mFooterLayout);
			mCurrentMode = mMode = MODE_PULL_DOWN_TO_REFRESH;
			setPadding(0, -mHeaderHeight, 0, 0);
		} else {
			mMode = MODE_BOTH;
			this.addView(mFooterLayout);
			setPadding(0, -mHeaderHeight, 0, -mHeaderHeight);

		}
	}

	public void setAdapter(BaseExpandableListAdapter adapter) {
		getRefreshableView().setAdapter(adapter);
	}

	public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
		getRefreshableView().setOnItemClickListener(listener);
	}

	public void setOnItemLongClickListener(
			AdapterView.OnItemLongClickListener listener) {
		getRefreshableView().setOnItemLongClickListener(listener);
	}

	public void setOnChildClickListener(
			OnChildClickListener onChildClickListener) {
		getRefreshableView().setOnChildClickListener(onChildClickListener);

	}

	public void setOnGroupClickListener(
			OnGroupClickListener onGroupClickListener) {
		getRefreshableView().setOnGroupClickListener(onGroupClickListener);
	}

	public void setOnGroupExpandListener(OnGroupExpandListener onGroupExpandListener){
		getRefreshableView().setOnGroupExpandListener(onGroupExpandListener);
	}

	public void setOnGroupCollapseListener(OnGroupCollapseListener onGroupCollapseListener){
	getRefreshableView().setOnGroupCollapseListener(onGroupCollapseListener);
}

	public void showRefreshView() {
		setHeaderRefreshingInternal(true);
	};

	public ListView getActureListView() {
		return getRefreshableView();
	}
}